$(function () {
    $('#roleid').on('change', function () {
        xw_loader.start();

        var data = {};
        data.roleID = $(this).find('option:selected').val();
        $.ajax({
            url: load_url,
            type: "POST",
            data: data,
            cache: false
        }).done(function (msg) {
            console.log(msg);
            $('input:radio[value=0]').each(function () {
                $(this).click();
            });

            xw_loader.end();
            if (msg.result === 1) {

                var list = msg.list;
				
                if (list != null)
                {
					$('#other').val(list[11].other);
                    for (var i = 0, len = list.length; i < len; i++)
                    {
						
                        $('.block_permission_inner').each(function () {
                            if ($(this).attr('data-id') == list[i].moduleID)
                            {
								
                                $(this).find('input').each(function () {
                                    if ($(this).val() == list[i].priv)
                                    {
                                        $(this).click();
                                    }
                                });
                            }
                        });
                    }
                }

            }
//            else {
//                swal('获取数据失败','','error');
//            }

        }).error(function (ex) {
            xw_loader.end();
            swal('获取数据错误', '', 'error');

        });
    });

    $('#save_role_priv').on('click', function () {
        if ($('#roleid').find('option:selected').val() == -1)
        {
            swal('请选择系统角色!', '', 'warning');
            return;
        }
        xw_loader.start();
        var data = {};
        data.roleID = $('#roleid').find('option:selected').val();
        data.list = [];
        $('.block_permission').each(function (index, item) {
            var index = index;
            data.list[index] = {};
            data.list[index].value = [];
            data.list[index].key = $(this).attr('data-id');
            $(item).find('.block_permission_inner').each(function (index2, item2) {
                data.list[index].value[index2] = {};
                data.list[index].value[index2].id = $(this).attr('data-id');
                data.list[index].value[index2].code = $(this).attr('data-code');
                data.list[index].value[index2].value = $(this).find('input:checked').val();
				data.list[index].value[index2].other = $('#other').val();
            });
        });
        console.log(data);

        $.ajax({
            url: post_url,
            type: "POST",
            data: data,
            cache: false
        }).done(function (msg) {
            console.log(msg);
            xw_loader.end();
            
            if (msg.result === 1) {

                swal('设置成功', '', 'success');

            }
            else {
                swal('保存失败：' + msg.desc , '', 'error');
            }

        }).error(function (ex) {
            xw_loader.end();
            swal('获取数据错误', '', 'error');

        });
    });



});